package com.youyou.action;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Dog {
    //execution作为切点的表达式
    @Pointcut("within(com.youyou.action.*)&&!within(com.youyou.action.LoginAction.*) &&!within(com.youyou.action.TestAction.*)")
    public void ml(){}
        //环绕通知
        @Around("ml()")
            public Object around (ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("连接点"+pjp);
            System.out.println("目标"+pjp.getTarget());
            //获取方法的前面
            Signature signature=pjp.getSignature();
            MethodSignature methodSignature= (MethodSignature) signature;
            String[] argsNames=methodSignature.getParameterNames();
            int accountArgs_index=-1;
            for (int i=0;i<argsNames.length;i++) {
                if (argsNames[i].equals("account")){
                    accountArgs_index=i;
                }
            }
            if (accountArgs_index!=-1){
                if (pjp.getArgs()[accountArgs_index]==null){
            return "redirect:loginView?message=LOGIN_TIME_OUT";
                }
            }
            System.out.println("accountArgs_index"+accountArgs_index);
Object result=pjp.proceed();
return result;
//            if (pjp.getTarget() instanceof  com.youyou.action.LoginAction){
//                System.out.println("not loginView.please check out session");
//                Object result=pjp.proceed();
//                return  result;
//            }else{
//                Object[] args=pjp.getArgs();
//                for (Object session: args){
//                    if (session==null){
//                        return "redirect:loginView?message=LOGIN_TIMW_OUT";
//                    }
//                }
//                Object result=pjp.proceed();
//                System.out.println("人返回的结果");
//                return result;
//            }
//    }
//@AfterReturning("ml()")
//public Object ok(){
//    System.out.println("AfterReturning");
//    return  null;
//}
//@After("ml()")
//    public Object endbye(){
//    System.out.println("after say byebye");
//    return  null;
//}
//@Before("ml()")
//    public Object beforesayhello(){
//    System.out.println("before");
//    return null;
//}

}
}
